/**
 * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.afterturn.easypoi.excel.export.styler;

import cn.afterturn.easypoi.util.PoiPublicUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;

import java.util.*;

/**
 * 抽象接口提供两个公共方法
 *
 * @author JueYue 2015年1月9日 下午5:48:55
 */
public abstract class AbstractExcelExportStyler implements IExcelExportStyler {
    //单行
    protected CellStyle stringNoneStyle;
    protected CellStyle stringNoneWrapStyle;
    //间隔行
    protected CellStyle stringSeptailStyle;
    protected CellStyle stringSeptailWrapStyle;

    protected Map<String,CellStyle> numberStylesMap ;

    protected Workbook workbook;

    protected DataFormat dataFormat;

    protected static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");

    protected void createStyles(Workbook workbook) {
        this.stringNoneStyle = stringNoneStyle(workbook, false);
        this.stringNoneWrapStyle = stringNoneStyle(workbook, true);
        this.stringSeptailStyle = stringSeptailStyle(workbook, false);
        this.stringSeptailWrapStyle = stringSeptailStyle(workbook, true);
        this.workbook = workbook;
        this.dataFormat = workbook.createDataFormat();
        this.numberStylesMap = buildNumberStyles();
    }

    private Map<String,CellStyle> buildNumberStyles() {
        Map<String,CellStyle> numberStyles = new LinkedHashMap<>();

        String text1 = "########0";
        String text2 = "###,###,##0";
        String text0 = "0.0";
        for(int i=0;i<=8 ; i++){
            String s1 = text1;
            String s2 = text2;
            String s0 = text0;
            if(i > 0){
                String xsw = PoiPublicUtil.rept("0" , i);
                s1 += "." + xsw;
                s2 += "." + xsw;
                s0 += "." + xsw;
            }
            numberStyles.put(s1,buildNumberStyle(s1));
            numberStyles.put(s2,buildNumberStyle(s2));
            numberStyles.put(s0+"%",buildNumberStyle(s0+"%"));
        }
        return numberStyles;
    }

    protected abstract CellStyle buildNumberStyle(String text);

    @Override
    public CellStyle getStyles(boolean noneStyler, ExcelExportEntity entity) {
        if (noneStyler && (entity == null || entity.isWrap())) {
            return stringNoneWrapStyle;
        }
        if (noneStyler) {
            return stringNoneStyle;
        }
        if (noneStyler == false && (entity == null || entity.isWrap())) {
            return stringSeptailWrapStyle;
        }
        return stringSeptailStyle;
    }

    @Override
    public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) {
        return getStyles(dataRow % 2 == 1, entity);
    }

    public CellStyle getNumberStyles(boolean noneStyler, ExcelExportEntity entity){
        if(StringUtils.isEmpty(entity.getNumFormat())){
            return numberStylesMap.values().stream().findFirst().get();
        }else{
            CellStyle cellStyle = this.numberStylesMap.get(entity.getNumFormat());
            if(cellStyle == null){
                cellStyle = buildNumberStyle(entity.getNumFormat());
            }
            return cellStyle;
        }
    }


    public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
        return null;
    }

    public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
        return null;
    }


    @Override
    public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) {
        return null;
    }

    public Map<String, CellStyle> getNumberStylesMap() {
        return numberStylesMap;
    }
}
